program subprocess;

{$mode objfpc}{$H+}
{$i ..\orcis.inc}

uses {$IFDEF UNIX} {$IFDEF UseCThreads}
    cthreads, {$ENDIF} {$ENDIF}
    {$ifdef lazcef}
    cef3types, cef3api
    , cef3own, cef3intf, cef3ref, cef3lib
    ;
    {$endif}
    {$ifdef wacef}
    WACefTypes, WACefLib, WACefCExports;
    {$endif}

type

    { TCustomRenderProcessHandler }

    TCustomRenderProcessHandler = class(TCefRenderProcessHandlerOwn)
    protected
        procedure OnWebKitInitialized; override;
        function OnBeforeNavigation(const browser: ICefBrowser; const frame: ICefFrame;
            const request: ICefRequest; const navigation_type: TCefNavigationType;
            const is_redirect: Boolean): boolean; override;
        //function OnProcessMessageReceived(const browser: ICefBrowser; sourceProcess: TCefProcessId;
        //    const message: ICefProcessMessage): boolean; override;
    end;


var
    Args: TCefMainArgs;
    //brw: ICefBrowser;
    app: TInternalApp;

    //procedure EventListenerProc(const event: ICefDomEvent);
    //var
    //    msg: ICefProcessMessage;
    //begin
    //    msg := TCefProcessMessageRef.New('mouseover');
    //    msg.ArgumentList.SetString(0, event.Target.AsMarkup);
    //    brw.SendProcessMessage(PID_BROWSER, msg);
    //end;

    //procedure VisitDomProc(const doc: ICefDomDocument);
    //begin
    //    doc.Body.AddEventListenerProc('mouseover', True, @EventListenerProc);
    //end;

    { TCustomRenderProcessHandler }

    procedure TCustomRenderProcessHandler.OnWebKitInitialized;
    begin
        inherited OnWebKitInitialized;
    end;

    function TCustomRenderProcessHandler.OnBeforeNavigation(const browser: ICefBrowser;
    const frame: ICefFrame; const request: ICefRequest; const navigation_type: TCefNavigationType;
    const is_redirect: Boolean): boolean;
    var
        msg: ICefProcessMessage;
    begin
        if navigation_type = NAVIGATION_LINK_CLICKED then
        begin
            msg := TCefProcessMessageRef.New('navigation');
            msg.ArgumentList.SetString(0, request.Url);
            Result := browser.SendProcessMessage(PID_BROWSER, msg);
            Result := True;
        end
        else
            Result := inherited OnBeforeNavigation(browser, frame, request, navigation_type, is_redirect);
    end;



    //function TCustomRenderProcessHandler.OnProcessMessageReceived(const browser: ICefBrowser;
    //    sourceProcess: TCefProcessId; const message: ICefProcessMessage): boolean;
    //
    //    procedure Log(const AMsg: string);
    //    var
    //        f: TextFile;
    //    begin
    //        AssignFile(f, 'sub.log');
    //        Append(f);
    //        Writeln(f, AMsg);
    //        CloseFile(f);
    //    end;
    //
    //begin
    //    Log(message.Name);
    //    if (message.Name = 'visitdom') then
    //    begin
    //        brw := browser;
    //        browser.MainFrame.VisitDomProc(@VisitDomProc);
    //        Result := True;
    //    end
    //    else
    //        Result := inherited OnProcessMessageReceived(browser, sourceProcess, message);
    //end;

begin
    CefRenderProcessHandler := TCustomRenderProcessHandler.Create;
    CefBrowserProcessHandler := TCefBrowserProcessHandlerOwn.Create;
    CefLoadLibrary;
    app := TInternalApp.Create;
    {$IFDEF WINDOWS}
    Args.instance := HINSTANCE();

    Halt(cef_execute_process(@Args, CefGetData(app), nil));
    {$ELSE}
    Args.argc := argc;
    Args.argv := argv;

    Halt(cef_execute_process(@Args, CefGetData(app)));
    {$ENDIF}
end.
